import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_levin/config/router_manager.dart';
import 'package:flutter_levin/config/storage_manager.dart';
import 'package:flutter_levin/config/ui_adapter_config.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

// ignore: library_prefixes
import 'config/router_manager.dart' as RouterManager;

void main() async {
  final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();

  await StorageManager.init();

  
  binding
    ..attachRootWidget(binding.wrapWithDefaultView(const MyApp()))
    ..scheduleWarmUpFrame();
  // widgetsBinding
  //   ..scheduleAttachRootWidget(widgetsBinding.wrapWithDefaultView(const MyApp()))
  //   ..scheduleWarmUpFrame();
  // runApp(const MyApp());
  // ToolbarUtils.setBarStatus(true);
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return RefreshConfiguration(
      hideFooterWhenNotFull: true,
      enableBallisticLoad: true,
      headerBuilder: () => const ClassicHeader(
        releaseText: '松开刷新',
        refreshingText: '正在加载...',
        failedText: '加载失败!!!',
        completeText: '加载完成',
        idleText: '下拉刷新',
      ),
      footerBuilder: () => const ClassicFooter(
        noDataText: '我是有底线的~',
        loadingText: '正在加载',
        failedText: '加载失败!!!',
        idleText: '上拉加载更多',
        canLoadingText: '松开开始加载',
      ),
      child: MaterialApp(
        title: 'flutter levin',
        debugShowCheckedModeBanner: false,
        darkTheme: _darkTheme(ThemeData.dark()),
        theme: _theme(ThemeData.light()),
        themeMode: ThemeMode.system,
        onGenerateRoute: RouterManager.Router.generateRouter,
        initialRoute: RouteName.splash,
        builder: EasyLoading.init(),
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate
        ],
        supportedLocales: const [
          Locale('en'),
          Locale('zh'),
        ],
      ),
    );
  }

  _theme(theme) => theme.copyWith(
        colorScheme: const ColorScheme.light().copyWith(
          primary: Colors.blue,
          secondary: Colors.black,
          onSecondary: Colors.blue,
          brightness: Brightness.light,
          onPrimary: Colors.white,
          tertiary: const Color(0x80FFFFFF),
          background: Colors.grey[100],
        ),
        iconTheme: IconThemeData(color: Colors.grey[900]),
        textTheme: TextTheme(
            titleLarge: const TextStyle(fontSize: 16, color: Colors.black),
            titleMedium: const TextStyle(fontSize: 14, color: Colors.black),
            titleSmall: const TextStyle(fontSize: 12, color: Colors.black),
            bodyLarge: TextStyle(color: Colors.grey[900], fontSize: 16),
            bodyMedium: TextStyle(color: Colors.grey[900], fontSize: 14)),
        inputDecorationTheme: const InputDecorationTheme(
            labelStyle: TextStyle(color: Colors.black, fontSize: 14),
            hintStyle: TextStyle(color: Colors.black45, fontSize: 14)),
        appBarTheme: const AppBarTheme(
            elevation: 0,
            centerTitle: true,
            titleTextStyle: TextStyle(
                fontSize: 16, fontWeight: FontWeight.bold, color: Colors.black),
            iconTheme: IconThemeData(color: Colors.black),
            // color: Colors.black,
            backgroundColor: Colors.white),
        dividerTheme: const DividerThemeData(color: Colors.white24, space: 1),
        cupertinoOverrideTheme: CupertinoThemeData(
          primaryColor: Colors.white,
          textTheme: CupertinoTextThemeData(
              dateTimePickerTextStyle:
                  TextStyle(color: Colors.grey[900], fontSize: 12)),
        ),
      );

  _darkTheme(theme) => theme.copyWith(
        colorScheme: const ColorScheme.dark().copyWith(
          primary: Colors.blue,
          secondary: Colors.white,
          onSecondary: Colors.blue,
          brightness: Brightness.dark,
          onPrimary: Colors.black,
          tertiary: const Color(0x80000000),
          error: Colors.redAccent,
          background: Colors.grey[900],
        ),
        iconTheme: IconThemeData(color: Colors.grey[100]),
        textTheme: TextTheme(
            titleLarge: const TextStyle(fontSize: 16, color: Colors.white),
            titleMedium: const TextStyle(fontSize: 14, color: Colors.white),
            titleSmall: const TextStyle(fontSize: 12, color: Colors.white),
            bodyLarge: TextStyle(color: Colors.grey[100], fontSize: 16),
            bodyMedium: TextStyle(color: Colors.grey[100], fontSize: 14)),
        inputDecorationTheme: const InputDecorationTheme(
            labelStyle: TextStyle(color: Colors.white, fontSize: 14),
            hintStyle: TextStyle(color: Colors.white70, fontSize: 14)),
        appBarTheme: const AppBarTheme(
            elevation: 0,
            centerTitle: true,
            titleTextStyle: TextStyle(
                fontSize: 16, fontWeight: FontWeight.bold, color: Colors.white),
            iconTheme: IconThemeData(color: Colors.white),
            // color: Colors.black,
            backgroundColor: Colors.black),
        dividerTheme: const DividerThemeData(color: Colors.black26, space: 1),
        cupertinoOverrideTheme: CupertinoThemeData(
          primaryColor: Colors.black,
          textTheme: CupertinoTextThemeData(
              dateTimePickerTextStyle:
                  TextStyle(color: Colors.grey[100], fontSize: 12)),
        ),
      );
}
